<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Repositories\TermRepository;

use App\Request\TermRequest;


class TermsController extends Controller
{
    private $term;
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct(TermRepository $term)
    {
        // 验证方式
        // $this->middleware('auth:api');       
        $this->term = $term; 
    }

    public function index()
    {
        $list  =  $this->term->all();
        return response()->json([
            'errors' => false,
            'count' => $list->count(),
            'data' => $list
        ]);
    }

    /**
     * Show the form for creating a nrew resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(TermRequest $request)
    {
        $term = $this->term->create($request->all());
        return response()->json([
            'error' => false,
            'message' => "新建成功",
            'data' => $term,
        ]);
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $term = $this->term->find($id);
         return response()->json([
             'errors' => false,
             'data' => $term
         ]);

        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $term = $this->term->find($id);
        $newTerm = $this->term->update($term, $request->all());
        return response()->json([
            'errors'=> false,
            'data' => $newTerm
        ]);
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $term = $this->term->find($id);
        $ret = $this->term->destroy($term);
        return response()->json([
            'errors' => false,
            'data' => $ret
        ]);
        //
    }
}
